package com.cjm.one.system.controller;


import com.cjm.one.common.annotation.IgnoreTenantForAdmin;
import com.cjm.one.common.result.Result;
import com.cjm.one.common.utils.AuthSysUserContextHolder;
import com.cjm.one.model.auth.system.dto.LoginDto;
//import com.cjm.one.security.annotation.Within;
import com.cjm.one.model.system.entity.SysResource;
import com.cjm.one.model.system.entity.SysRole;
import com.cjm.one.model.system.entity.SysUserInfo;
import com.cjm.one.model.system.vo.SysResourceVo;
import com.cjm.one.security.annotation.Within;
import com.cjm.one.system.service.AuthSystemService;
import com.cjm.one.system.service.SysRoleService;
import com.cjm.one.system.service.SysUserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "后台系统相关接口")
@RestController
@Slf4j
@RequestMapping("/admin/auth/system")
@IgnoreTenantForAdmin
public class AuthSystemController {

    @Resource
    private AuthSystemService authSystemService ;

    @PostMapping("login")
    @Operation(summary = "后台系统登录")
    public Result<String> login(@RequestBody LoginDto loginDto){
        log.info("系统登录：LoginDto:{}",loginDto);
        return Result.ok(authSystemService.login(loginDto));
    }
    @PostMapping("logout")
    @Operation(summary = "后台系统退出登录")
    public Result<Boolean> logout(){
        log.info("系统用户退出登录");
        //TODO: 退出登录
        return Result.ok(true);
    }

    /**
     * @description:根据用户id获取用户权限列表
     * @author: cjm
     * @date: 2024/9/20 16:19
     * @param: [userId]
     * @return: com.cjm.one.common.result.Result<java.util.List<java.lang.String>>
     **/
    @Within
    @PostMapping("findUserPermsList/{userId}")
    @Operation(summary = "获取用户权限列表")
    @IgnoreTenantForAdmin
    public Result<List<String>> findUserPermsList(@PathVariable("userId") Long userId){
        log.info("获取用户权限列表,用户id:{}",userId);
        return Result.ok(authSystemService.findUserPermsList(userId));
    }

    @GetMapping("userInfo")
    @Operation(summary = "获取当前登录用户信息")
    @IgnoreTenantForAdmin
    public Result<SysUserInfo> userInfo(@RequestHeader("Authorization") String Authorization){
        log.info("获取当前登录用户信息");
        SysUserInfo sysUserInfo = authSystemService.getCurrentUser(Authorization);
        return Result.ok(sysUserInfo);
    }

    @GetMapping("menuTree")
    @Operation(summary = "获取当前登录用户的菜单树")
    public Result<List<SysResourceVo>> getAllMenuList() {
        Long id = AuthSysUserContextHolder.getAuthContextSysUser().getId();
        log.info("获取当前登录用户的菜单树，用户id:{}", id);
        return Result.ok(authSystemService.getSysResourceList(id));

    }



}
